package com.budwk.app.base.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.fetchdata.models.HttpClientUtil;
import com.budwk.app.wxprogram.models.Wx_area;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.mvc.annotation.Param;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: CoordinateUtils
 * @Author Tan
 * @Date: 2023/4/11 14:32
 * @Version 1.0
 */

public class CoordinateUtils {
    private static final Dao dao = null;




    public static Map gtelatlng(@Param("addr") String address) {
        Map map=new HashMap();
        List<Wx_area> fetch=dao.query(Wx_area.class, Cnd.where("whole_name","=",address));

        String res=address;
        String b="，";
        int count= (res.length()-res.replace(b,"").length())/b.length();
        if (count!=0) {
            for (int i = count; i > 0; i--) {
                if (fetch.size() == 0 || fetch == null) {
                    address = address.substring(0, address.indexOf("，"/*,address.indexOf("，")+i*/));

                    if (address.charAt(address.length() - 1) == '，') {
                        address = address.substring(0, address.length() - 1);
                    }


                    fetch = dao.query(Wx_area.class, Cnd.where("whole_name", "=", address));


                }

                map.put("lat",fetch.get(0).getLat());
                map.put("lng",fetch.get(0).getLon());

            }
        }



        return  map;
    }



    //腾讯地图
    public static Map getLatAndLngByAddrforTengxun(@Param("addr") String addr) {
        Map map=new HashMap();
        try {
            String result = "";


            addr = addr.replace(" ", "").replace("#", "").replace("中国", "");
            String queryStr = "https://apis.map.qq.com/ws/district/v1/search?&keyword="+addr+"&key=IZABZ-V7YKJ-AVJFI-XHJ4U-VABZ2-CXBMA";
            /**
             *HttpClientUtil获取路径：
             *https://blog.csdn.net/weixin_40052298/article/details/123347111?spm=1001.2014.3001.5501
             */
            String info = HttpClientUtil.get(queryStr);
            if (StringUtils.isNotBlank(info)) {
                JSONObject resultJson = JSON.parseObject(info);
                if (!"参数错误".equals(resultJson.get("message"))) {

                    JSONArray locationObj = (JSONArray) resultJson.get("result");
                    //JSONArray jsonArray=JSONArray.parseArray(String.valueOf(locationObj));
                    JSONArray jsonarray= (JSONArray) locationObj.get(0);
                    JSONObject jsonObject= (JSONObject) jsonarray.get(0);


                    JSONObject locationObjlocation = (JSONObject) jsonObject.get("location");
                    //行政区域码
                    Object idObj =  jsonObject.get("id");
                    //纬度
                    String lat = locationObjlocation.get("lat") + "";
                    //经度
                    String lng = locationObjlocation.get("lng") + "";
                    //Map polygon=getPolygonforTengxun(idObj.toString());

                    map.put("lng",lng);
                    map.put("lat",lat);
                    map.put("code",idObj.toString());

                    result = "经度：" + lng + "\n纬度：" + lat;
                }
            }/*"经度：106.566386 纬度：26.620078"*/
            return map;
        } catch (Exception e) {
            return  map;
        }
    }


    public static Map getPolygonforTengxun(@Param("code") String code) {
        Map map=new HashMap();
        try {
            String result = "";


            //addr = addr.replace(" ", "").replace("#", "").replace("中国", "");
            String queryStr = "https://apis.map.qq.com/ws/district/v1/search?&keyword="+code+"&key=IZABZ-V7YKJ-AVJFI-XHJ4U-VABZ2-CXBMA&get_polygon=2";
            /**
             *HttpClientUtil获取路径：
             *https://blog.csdn.net/weixin_40052298/article/details/123347111?spm=1001.2014.3001.5501
             */
            String info = HttpClientUtil.get(queryStr);
            if (StringUtils.isNotBlank(info)) {
                JSONObject resultJson = JSON.parseObject(info);
                if (!"参数错误".equals(resultJson.get("message"))) {
                    JSONArray locationObj = (JSONArray) resultJson.get("result");
                    //JSONArray jsonArray=JSONArray.parseArray(String.valueOf(locationObj));
                    JSONArray jsonarray= (JSONArray) locationObj.get(0);
                    JSONObject jsonObject= (JSONObject) jsonarray.get(0);


                    JSONArray locationObjlocation = (JSONArray) jsonObject.get("polygon");

                    String polygon ="";
                    for (int i=0;i<locationObjlocation.size();i++){

                        polygon += locationObjlocation.get(i).toString();
                    }

                    //polygon

                    //经度
                    map.put("polygon",polygon);


                    result = "polygon：" + polygon ;
                }
            }/*"经度：106.566386 纬度：26.620078"*/
            return map;
        } catch (Exception e) {
            return  map;
        }
    }









    //天地图
    public static Map getLatAndLngByAddr(@Param("addr") String addr) {
        Map map=new HashMap();
        try {
            String result = "";

            addr = addr.replace(" ", "").replace("#", "").replace("中国", "");
            String queryStr = "http://api.tianditu.gov.cn/geocoder?ds=%7B'keyWord':'" + addr + "'%7D&tk=" + "25a77f643d94d6acbd6c1b79cfeeda69";
            /**
             *HttpClientUtil获取路径：
             *https://blog.csdn.net/weixin_40052298/article/details/123347111?spm=1001.2014.3001.5501
             */
            String info = HttpClientUtil.get(queryStr);
            if (StringUtils.isNotBlank(info)) {
                JSONObject resultJson = JSON.parseObject(info);
                if (!"无结果".equals(resultJson.get("msg"))) {
                    JSONObject locationObj = (JSONObject) resultJson.get("location");
                    //纬度
                    String lat = locationObj.get("lat") + "";
                    //经度
                    String lng = locationObj.get("lon") + "";
                    map.put("lng",lng);
                    map.put("lat",lat);



                    result = "经度：" + lng + "\n纬度：" + lat;
                }
            }/*"经度：106.566386 纬度：26.620078"*/
            return map;
        } catch (Exception e) {
            return  map;
        }
    }
}
